package com.topscore.uke.mvp.presenters;

import android.text.Editable;
import android.text.TextWatcher;

import com.google.gson.Gson;
import com.topscore.uke.entities.UserInfo;
import com.topscore.uke.mvp.contractors.LoginContractor;
import com.topscore.uke.mvp.model.LoginModel;
import com.topscore.uke.network.BaseResponse;
import com.topscore.uke.network.request.LoginBean;
import com.topscore.uke.utils.StringUtil;
import com.topscore.uke.utils.UserInfoManager;


/**
 * Created by 71468 on 2017/9/13.
 */

public class LoginPresenter implements LoginContractor, LoginContractor.Presenter {

    private LoginView mView;
    private LoginBean mRequest;
    private Model mModel;

    public LoginPresenter(LoginView mView) {
        this.mView = mView;
        this.mRequest = new LoginBean();
        this.mModel = new LoginModel(new LoginListener() {
            @Override
            public void onLoginSuccess(BaseResponse<UserInfo> response) {
                saveUserInfo(response.getData());
                if (StringUtil.isEmpty(response.getData().getMemberCardNumber())){
                    mView.onMemberUnBound();
                }else {
                    mView.onLoginSuccess();
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.onFail(errorCode, errorMessage);
            }

            @Override
            public void onComplete() {
                mView.onLoadFinished();
            }
        });
        mView.getLoginEdt().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mRequest.setLoginName(editable.toString());
                setupLoginClickable();
            }
        });
        mView.getPwdEdt().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mRequest.setPassword(editable.toString());
                setupLoginClickable();
            }
        });
    }

    private void saveUserInfo(UserInfo data) {
        UserInfoManager.updateUserInfo(data);
    }

    private void setupLoginClickable() {
        if (mRequest.getLoginName() != null
                && mRequest.getLoginName().length() > 0
                && mRequest.getPassword() != null
                && mRequest.getPassword().length() > 0){
            mView.setLoginClickable(true);
        }else {
            mView.setLoginClickable(false);
        }
    }


    @Override
    public void login() {
        if (mRequest.getLoginName() != null
                && mRequest.getLoginName().length() > 0
                && mRequest.getPassword() != null
                && mRequest.getPassword().length() > 0) {
            mView.onLoading("正在登陆");
            mModel.login(mRequest);
        } else if (mRequest.getLoginName() == null
                || mRequest.getLoginName().length() == 0) {
            mView.onFail(501, "请输入账号");
        } else if (mRequest.getPassword() == null
                || mRequest.getPassword().length() == 0) {
            mView.onFail(502,"请输入密码");
        }else {
            mView.onFail(503,"请输入账号密码");
        }
    }
}
